<?php
/**
 * 门店模型
 * User: ws
 * Date: 2018/7/25
 * Time: 15:40
 */

namespace app\admin\model;
use think\Model;

class Store extends Model
{
    protected $autoWriteTimestamp = true;

    public function setStorePassAttr($value)
    {
        if (empty($value)) return '';
        return  strtolower(md5($value));
    }


    function add($params)
    {
        $result = $this->isUpdate(false)->allowField(true)->save($params);
        if($result){
            return 1;
        }else{
            return false;
        }
    }


    function edit($params)
    {
        $result = $this->isUpdate(true)->allowField(true)->save($params);
        if($result){
            return true;
        }else{
            return false;
        }
    }



    //门店
    function store_(){
        return $this->field('store_id,name')->paginate('100')->toArray();
    }




    //删除图片
    function del($where)
    {
        $Img = $this->where($where)->find();
        unlink('.'.$Img['img']);
    }



    //批量删除图片
    function dele($id)
    {
        $where['store_id'] = array('in',$id);
        $Img = $this->where($where)->select();
        foreach ($Img as $key=>$item){
            unlink('.'.$item['img']);
        }

    }



    /**
     * 批量操作
     * $act  操作类型 delete
     * $params 参数
     */
    function batches($act,$params){
        if($act == 'delete'){
            $result = $this->destroy($params);
        }
        if($result){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 获取列表
     * $whith_page 0不分页  1分页返回对象  2分页返回数组
     *
     */
    function get_list($where = '',$order = 'store_id desc',$page_size=15,$whith_page=0,$field= '*'){
        if(!$order){ $order = 'store_id desc';}
        if($whith_page == 0){
            $articles = $this->where($where)->field($field)->order($order)->limit($page_size)->select();
            return $articles;
        }
        $articles = $this->where($where)->field($field)->order($order)->paginate($page_size);
        if($whith_page == 1){
            return $articles;
        }
        $articles = $articles->toArray();
        return $articles;
    }
}